import '../common/common.dart';
import 'package:get_storage/get_storage.dart';

class _BoxKey {
  static const String appSetting = 'appSetting';
}

class _StorageKey {
  static const String userToken = 'userToken';
  static const String userFamilyId = 'userFamilyId';
  static const String knobControlEnable = 'knobControlEnable';
  static const String knobControlType = 'knobControlType';
  static const String onlyShowDevelopDevice = 'onlyShowDevelopDevice';
}

class DataManager {
  DataManager._();

  static final DataManager _instance = DataManager._();

  static DataManager get instance => _instance;

  /// 初始化容器
  Future<bool> initStorage() async {
    return await GetStorage.init(_BoxKey.appSetting);
  }

  /// 应用设置容器
  static GetStorage get appSettingBox => GetStorage(_BoxKey.appSetting);

  /// 用户Token
  final userToken = ReadWriteValue(_StorageKey.userToken, '', () => appSettingBox);
  /// 用户家庭
  final userFamilyId = ReadWriteValue(_StorageKey.userFamilyId, '', () => appSettingBox);

  /// 旋钮开关
  final knobControlEnable = ReadWriteValue(_StorageKey.knobControlEnable, false, () => appSettingBox);

  /// 旋钮控制类别 (枚举类型存储还是有点麻烦 https://github.com/jonataslaw/get_storage/issues/95)
  KnobControlType get knobControlType => _getKnobControlType();

  KnobControlType _getKnobControlType() {
    var typeIndex = appSettingBox.read<int>(_StorageKey.knobControlType);
    if (typeIndex == null) return KnobControlType.none;
    if (0 <= typeIndex && typeIndex < KnobControlType.values.length) {
      return KnobControlType.values[typeIndex];
    } else {
      return KnobControlType.none;
    }
  }

  set knobControlType(KnobControlType type) {
    appSettingBox.write(_StorageKey.knobControlType, type.index);
  }
  /// 是否只显示要对接的设备
  final onlyShowDevelopDevice = ReadWriteValue(_StorageKey.onlyShowDevelopDevice, false, () => appSettingBox);
}
